///////////////////////////////////////////////////////////
//  OQLMaxCondition.cpp
//  Implementation of the Class OQLMaxCondition
//  Created on:      23-sep-2007 21:26:38
///////////////////////////////////////////////////////////

#include "OQLMaxCondition.h"
#include "ResolveDBRequest.h"

#ifndef ZERO 
	#define ZERO 0
#endif


OQLMaxCondition::OQLMaxCondition(){

}



OQLMaxCondition::~OQLMaxCondition(){

}

ListPersistent OQLMaxCondition::resolve(OQLCondition* cond){
	ListPersistent list, retorno;
	Persistent* object = NULL;
	ResolveDBRequest res;
	std::string valor("");
	std::string atributo("");
	
	atributo += this->attribute[0].nombre;
	
	retorno.setAtributte(atributo);
	list.setAtributte(atributo);
	
	if(cond == NULL)
	{
		retorno = res.resolverMax((*this));
	}
	else
	{
		ListPersistent listDelete;
		list = cond->resolve(this->table);  // devuelve una lista ordenada por la condicion
		if (list.getSize() == 0)
			return retorno;
		// ahora ordeno la lista por el valor a devolver el minimo
		list.sort(atributo);
		object = list.getLast();
		valor = object->getValor(atributo);
		
		for(int i = ZERO; i < list.getSize(); i++)
		{
			Persistent * current = list.getPersistent(i);
			if(current->getValor(atributo) == valor)
				retorno.insertSort(current);
			else
				listDelete.insertSort(current);
		}
		listDelete.clear();
		
	}
		
	return retorno;
}



